<?php

namespace App\Http\Controllers\Backstage\User;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

//管理员模块
class UserManageController extends Controller
{
    // public function pwd(){
    //     $info=Hash::make('123456');
    //     return $info;
    // }
    // public function pwd(Request $request)
    // {
    //     $token = $request->query('token');
    //     $info = verifyBackstageToken($token);
    //     if(is_array($info)){
    //         return $info;
    //     };
    //     $data=success();
    //     return $data;
    // }
    //管理员管理
    public function usermanage(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $info = DB::table('admin')->get();
        if ($info) {
            $info = json_decode($info, true); //对象转成数组
            foreach ($info as $key => $value) {
                $info[$key]['time'] = date('Y-m-d', $value['time']);
            }
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }

    //复制密码并验证
    // public function verifycode(Request $request)
    // {
    //     $token = $request->input('token');
    //     //验证权限
    //     $verifytoken = verifytokens($token);
    //     if (isset($verifytoken)) {
    //         return $verifytoken;
    //     }
    //     $map['aid'] = $request->input('aid');
    //     $password = $request->input('code');
    //     $info = DB::table('admin')->where($map)->first();
    //     if ($info->password == $password) {
    //         $data = success();
    //         $pwd = Crypt::decryptString($password);
    //         $random = mt_rand(000000, 999999);
    //         $data['data'] = $pwd . $random;
    //     } else {
    //         $data = fail();
    //     }
    //     return $data;
    // }
    //新增管理员
    public function adduser(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $date['name'] = $request->input('name');
        $pwd = $request->input('password');
        $date['password'] = Hash::make($pwd);
        $date['role'] = $request->input('role');
        $date['type'] = 1;
        $date['time'] = time();
        $date['merchant_id'] = 0;

        $info = DB::table('admin')->insert($date);
        if ($info) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }

    //修改管理员信息
    public function changeManageInfo(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['aid'] = (int) $request->input('aid');
        $map['name'] = $request->input('name');
        if($request->input('password')){
            $password = $request->input('password');
            $map['password'] = Hash::make($password);
        }
        if($request->input('role')){
            $map['role'] = $request->input('role');
        }
        $oldpassword = $request->input('oldpassword');
        $info = DB::table('admin')->where('aid', $map['aid'])->first();
        // 如果有传密码验证
        if ($oldpassword&&$password) {
            if (Hash::check($oldpassword, $info->password)) {
                $up = DB::table('admin')->where('aid', $map['aid'])->update($map);
                if ($up) {
                    $data = success();
                    $data['msg'] = '更新用户信息成功';
                } else {
                    $data = fail();
                    $data['msg'] = '更新用户信息失败，请刷新重试';
                }
            } else {
                $data = fail();
                $data['msg'] = '旧密码验证失败，请重试';
            }
        }

        return $data;
    }

    //删除管理员
    public function deluser(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['aid'] = (int)$request->input('aid');
        if ($map['aid'] == 1) {
            $data = fail();
            $data['msg'] = '不允许删除初始管理员';
        } else {
            $date['type'] = 0;
            $del = DB::table('admin')->where($map)->update($date);
            if ($del) {
                $data = success();
            } else {
                $data = fail();
                $data['msg'] = '删除失败，请刷新重试';
            }
        }
        return $data;
    }
}
